package com.mcxx.modules.temporary.controller;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.mcxx.modules.temporary.service.TempQueryService;
import com.mcxx.modules.temporary.service.TempSalvationApplyService;
import com.mcxx.modules.miniSalvation.family.dto.BankAbutmentDTO;
import com.mcxx.modules.miniSalvation.family.entity.BankEntity;
import com.mcxx.modules.miniSalvation.family.service.read.BankReadService;
import com.mcxx.modules.miniSalvation.family.service.write.BankWriteService;
import com.mcxx.modules.temporary.family.dto.TempFamilyDTO;
import com.mcxx.modules.temporary.family.dto.TempMemberCardRtnDTO;
import com.mcxx.modules.temporary.family.dto.TmpFamilyMemberDTO;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberEntity;
import com.mcxx.modules.temporary.family.service.read.TmpFamilyMainReadService;
import com.mcxx.modules.temporary.family.service.read.TmpFamilyMemberReadService;
import com.mcxx.modules.temporary.family.service.write.TmpFamilyMemberWriteService;
import com.mcxx.modules.temporary.order.dto.ApplicationDTO;
import com.mcxx.modules.temporary.order.dto.ApplyRtnDTO;
import com.mcxx.modules.temporary.order.service.read.TmpOrderReadService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.validate.group.Creation;
import com.mcxx.validate.group.Update;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Slf4j
@Validated
@Api("临时救助申请控制类")
@RestService({"temporary/salvation/request/v2"})
public class TempSalvationRequestController extends DefaultRestService {
    @Autowired
    private TempSalvationApplyService tempSalvationApplyService;
    @Autowired
    private TmpFamilyMemberReadService familyMemberReadService;
    @Autowired
    private TmpFamilyMemberWriteService familyMemberWriteService;
    @Autowired
    private TmpOrderReadService tmpOrderReadService;
    @Autowired
    private TempQueryService tempQueryService;
    @Autowired
    private BankReadService bankReadService;
    @Autowired
    private BankWriteService bankWriteService;
    @Autowired
    private TmpFamilyMainReadService tmpFamilyMainReadService;


    @PostMapping("saveSalvationRequest")
    @ApiOperation("保存申请信息")
    public void saveSalvationRequest(@Validated(Creation.class) ApplicationDTO applyDto) {
        log.info("开始保存申请信息，参数是：{}", applyDto);
        BaseResult<ApplyRtnDTO> result = tempSalvationApplyService.saveSalvationRequest(applyDto, getCurUserAccount());
        setData(result.getData());
        setObjErrors(result.getCode(), result.getMsg());
        log.info("保存申请信息结束，参数是：{}", JSON.toJSONString(applyDto));
    }


    @PostMapping("modifySalvationRequest")
    @ApiOperation("更新申请信息")
    public void modifySalvationRequest(@Validated(Update.class) ApplicationDTO applyDto) {
        log.info("开始更新申请信息，参数是：{}", applyDto);
        BaseResult<ApplyRtnDTO> result = tempSalvationApplyService.saveSalvationRequest(applyDto, getCurUserAccount());
        setData(result.getData());
        setObjErrors(result.getCode(), result.getMsg());
        log.info("更新申请信息结束，参数是：{}", JSON.toJSONString(applyDto));
    }


    @GetMapping("getOrderDetailById")
    @ApiOperation("查看未办结业务详情")
    public void getOrderDetailById(@V({"notnull"}) String orderId) {
        log.info("查看未办结业务详情，参数：{}", orderId);
        ApplicationDTO applyDto = tmpOrderReadService.getDetailById(orderId);
        setData(applyDto);
        log.info("查看未办结业务详情完成，结果：{}", applyDto);
    }


    @GetMapping("listFamilyMember")
    @ApiOperation("根据familyId获取家庭成员列表")
    public void listFamilyMember(@V({"notnull"}) String familyId) {
        log.info("查询家庭成员列表，参数：{}", familyId);
        List<TmpFamilyMemberEntity> list = familyMemberReadService.listByFamilyId(familyId);
        setData(list);
        log.info("查询家庭成员列表完成，结果：{}", JSON.toJSONString(list));
    }


    @GetMapping("getFamilyMemberById")
    @ApiOperation("根据id查询指定家庭成员信息")
    public void getFamilyMemberById(@V({"notnull"}) String id) {
        TmpFamilyMemberEntity memberEntity = familyMemberReadService.getById(id);
        setData(memberEntity);
    }


    @RequestMapping("addFamilyMember")
    @ApiOperation("添加家庭成员")
    public void addFamilyMember(@Validated TmpFamilyMemberDTO dto) {
        dto.setIsSaveObject("0");
        log.info("开始添加家庭成员信息，参数是：{}", dto);
        dto.setBusinessType(Constant.BusinessCode.LINSHI);
        tempSalvationApplyService.addFamilyMember(dto, getCurUserAccount());
        log.info("添加家庭成员信息结束");
    }


    @RequestMapping("modifyFamilyMember")
    @ApiOperation("更新家庭成员信息")
    public void modifyFamilyMember(@Validated TmpFamilyMemberDTO dto) {
        log.info("开始更新家庭成员信息，参数是：{}", dto);
        dto.setBusinessType(Constant.BusinessCode.LINSHI);
        tempSalvationApplyService.updateFamilyMember(dto, getCurUserAccount());
        log.info("更新家庭成员信息结束");
    }


    @PostMapping("delFamilyMember")
    @ApiOperation("删除家庭成员信息")
    public void delFamilyMember(TmpFamilyMemberDTO dto) {
        log.info("开始删除临时救助家庭成员信息，参数是：{}", dto);
        tempSalvationApplyService.delete(dto, getCurUserAccount());
        log.info("删除临时救助家庭成员信息");
    }


    @PostMapping("changeFamilyMaster")
    @ApiOperation("户主变更")
    public void changeFamilyMaster(@V({"notnull"}) String familyId, String orderId, @V({"notnull"}) @RequestPart Map<String, String> relationMap) {
        log.info("开始变更户主，参数是：{},{},{}", familyId, orderId, relationMap);
        familyMemberWriteService.changeFamilyRelation(familyId, orderId, relationMap);
        log.info("开始变更户主结束");
    }


    @GetMapping("getFamilyRelationMap")
    @ApiOperation("获取家庭成员关系")
    public void getFamilyRelationMap(@V({"notnull"}) String familyId) {
        log.info("开始获取家庭成员关系，参数是：{}", familyId);
        List<TmpFamilyMemberEntity> relationMap = familyMemberReadService.getFamilyRelationMap(familyId);
        setData(relationMap);
        log.info("获取家庭成员关系结束，参数是：{}", relationMap);
    }


    @PostMapping("queryStateByIdCard")
    @ApiOperation("根据身份证号码查询状态信息（户主身份证校验）")
    public void queryStateByIdCard(@V({"notnull", "idcard"}) String idcard,String channel) {
        log.info("根据身份证号码查询状态信息（户主身份证校验），参数：{}", idcard);


        BaseResult<TempFamilyDTO> rstData = tempSalvationApplyService.getPersonInfoByIdCard(idcard,getCurUserAccount(),channel);
        setData(rstData.getData());
        if (!"0".equals(rstData.getCode())) {
            setObjErrors(rstData.getCode(), rstData.getMsg());
        }
        log.info("根据身份证号码查询状态信息（户主身份证校验）完成，结果：{}", JSON.toJSONString(rstData));
    }


    @PostMapping("queryMemberStateByIdCard")
    @ApiOperation("根据身份证号码查询状态信息（临时救助成员身份证校验）")
    public void queryMemberStateByIdCard(@V({"notnull", "idcard"}) String idcard) {
        log.info("根据身份证号码查询状态信息（临时救助成员身份证校验），参数：{}", idcard);
        BaseResult<Map> rstData = tempSalvationApplyService.queryMemberStateByIdCard(idcard);
        setData(rstData.getData());
        if (!"0".equals(rstData.getCode())) {
            setObjErrors(rstData.getCode(), rstData.getMsg());
        }
        log.info("根据身份证号码查询状态信息（临时救助成员身份证校验）完成，结果：{}", JSON.toJSONString(rstData));
    }


    @PostMapping("checkSelectedReason")
    @ApiOperation("根据身份证号码和申请原因查询救助次数")
    public void checkSelectedReason(@V({"notnull", "idcard"}) String idcard, @V({"notnull"}) String mainReason) {
        log.info("根据身份证号码和申请原因查询救助次数，参数：{},{}", idcard, mainReason);
        String year = DateUtil.format(new Date(), "yyyy");
        BaseResult<Integer> result = tempSalvationApplyService.countByIdCardAndReason(idcard, mainReason, year, getCurUserAccount());
        setData("count", result.getData());
        if (!"0".equals(result.getCode())) {
            setObjErrors(result.getCode(), result.getMsg());
        }
        log.info("根据身份证号码和申请原因查询救助次数完成，结果：{}", JSON.toJSONString(result));
    }


    @PostMapping("queryHandleStatusByIdCardNumber")
    @ApiOperation("按身份证号查询办理情况")
    public void queryHandleStatusByIdCardNumber(String idcard) {
        List<TempMemberCardRtnDTO> tempMemberCardRtnDTOS = tempSalvationApplyService.queryMemberByCardId(idcard);
        setData(tempMemberCardRtnDTOS);
    }

    @PostMapping("changeMemberApply")
    @ApiOperation("申请人变更")
    public void changeMemberApply(@V({"notnull"}) String familyId,@V({"notnull"}) String orderId, @V({"notnull"}) @RequestPart Map<String,String> relationMap) {
        log.info("开始变更申请人，参数是：{},{},{}", familyId, orderId, relationMap);
        tempSalvationApplyService.changeMemberApply(familyId, orderId, relationMap,getCurUserAccount());
        log.info("开始变更申请人结束");
    }

    @RequestMapping("backUpHtml")
    @ApiOperation("低保信息备份")
    public void backHtml(@V({"notnull"}) String orderId,@V({"notnull"}) String familyId){
        log.info("低保信息备份开始,参数：{},{}",orderId,familyId);
        String res = tempSalvationApplyService.backupHtml(orderId,familyId);
        setData(res);
        log.info("低保信息备份结束,结果是:{}",res);
    }

    @GetMapping("getFamilyApply")
    @ApiOperation("获取家庭成员申请人")
    public void getFamilyApply(@V({"notnull"}) String familyId) {
        log.info("开始获取家庭成员关系，参数是：{}", familyId);
        List<TmpFamilyMemberEntity> relationMap = familyMemberReadService.getFamilyApplyMapByFamilyId(familyId);
        setData(relationMap);
        log.info("获取家庭成员关系结束，参数是：{}", relationMap);
    }

    @PostMapping("getApplyByFamilyId")
    @ApiOperation("根据家庭id获取申请人信息")
    public void getApplyByFamilyId(@V({"notnull"}) String familyId) {
        log.info("根据家庭id获取申请人信息，参数是：{}", familyId);
        TmpFamilyMemberEntity entity = familyMemberReadService.getApplyByFamilyId(familyId);
        setData(entity);
        log.info("根据家庭id获取申请人信息结束");
    }

    @PostMapping("tempSaveInHouse")
    @ApiOperation("暂存入户调查")
    public void tempSaveInHouse(ApplicationDTO applyDto){
        log.info("暂存入户调查，参数是：{}", applyDto);
        ApplyRtnDTO applyRtnDTO = tempSalvationApplyService.tempSaveInHouse(applyDto, getCurUserAccount());
        setData(applyRtnDTO);
        log.info("暂存入户调查，结果是：{}", JSON.toJSONString(applyDto));
    }

    @GetMapping("getMemberByIdAndOrderId")
    @ApiOperation("根据id查询指定家庭成员信息")
    public void getMemberByIdAndOrderId(@V({"notnull"}) String id, @V({"notnull"}) String orderId) {
        log.info("根据memberId和orderId查询指定家庭成员信息，参数：id：{}，orderId：{}", id, orderId);
        TmpFamilyMemberDTO tmpFamilyMemberDTO = tempQueryService.getFamilyMemberBankInfo(id, orderId,Constant.BusinessCode.LINSHI);
        setData(tmpFamilyMemberDTO);
        log.info("根据id查询指定家庭成员信息完成，结果：{}", JSON.toJSONString(tmpFamilyMemberDTO));
    }


    @PostMapping("completeInHouse")
    @ApiOperation("结束入户调查")
    public void completeInHouse(ApplicationDTO applyDto){
        log.info("结束入户调查，参数是：{}", applyDto);
        ApplyRtnDTO applyRtnDTO = tempSalvationApplyService.completeInHouse(applyDto, getCurUserAccount());
        setData(applyRtnDTO);
        log.info("结束入户调查，结果是：{}", JSON.toJSONString(applyDto));
    }


    @GetMapping("getFamilyBankInfoById")
    @ApiOperation("根据familyId查询指定家庭银行卡")
    public void getFamilyBankInfoById(@V({"notnull"}) String familyId) {
        log.info("根据familyId查询指定家庭银行卡，参数是：{}", familyId);
        List<BankEntity> list = bankReadService.getBankByfamilyId(familyId,Constant.BusinessCode.LINSHI);
        if(!list.isEmpty()){
            setData(list.get(0));
        }
        log.info("根据familyId查询指定家庭银行卡结束，参数是：{}", familyId);
    }


    @GetMapping("gainBankInfo")
    @ApiOperation("获取财政银行卡信息")
    public void gainBankInfo(@Validated BankAbutmentDTO dto) {
        log.info("开始获取财政银行卡信息，参数是：{}", dto);
        Map<String, String> map = tempSalvationApplyService.gainBankInfo(dto,getCurUserAccount());
        setData(map);

        log.info("获取财政银行卡信息结束");
    }


    @PostMapping("resetBankByFamilyId")
    @ApiOperation("将家庭的银行卡信息置为无效")
    public void stopBankByFamilyId(String familyId,String memberId) {
        log.info("将家庭的银行卡信息置为无效，参数是：{},{}", familyId,memberId);
        int i = bankWriteService.resetBankAccountByFamilyId(familyId,memberId);
        setData(i);
        log.info("获取财政银行卡信息结束");
    }

    @PostMapping("getPhoneByFamilyId")
    @ApiOperation("查询家庭表中申请人的手机号")
    public void getPhoneByFamilyId(String familyId) {
        log.info("查询家庭表中申请人的手机号，参数是：{}", familyId);
        String phone = tmpFamilyMainReadService.selectPhoneByFamilyId(familyId);
        setData(phone);
        log.info("查询家庭表中申请人的手机号结束,结果是：{}",phone);
    }
}
